﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class Ortu : Form
    {
        private OrtuAdd add = null;
        private OrtuEdit edit = null;
        private DataTable dTable = null;
        private BindingSource bindingSource1 = new BindingSource();

        public Ortu()
        {
            InitializeComponent();
            Style.setDatagridviewStyle(dataGridView1);
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            if (this.add == null || this.add.IsDisposed)
                add = new OrtuAdd(this);

            //check whether the form has been opened or not
            if (((Main)this.MdiParent).isOpenForm(add))
            {
                add = null;
                return;
            }

            add.MdiParent = this.MdiParent;
            add.Show();
        }

        private void Ortu_Load(object sender, EventArgs e)
        {
            this.update();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.add == null || this.add.IsDisposed)
                add = new OrtuAdd();

            this.add.MdiParent = this.MdiParent;
            this.add.Show();
            this.add.Activate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.update();
        }

        private void update()
        {
            this.dataGridView1.Columns.Clear();

            dTable = Database.getInstance().query("select * from Parent");

            bindingSource1.DataSource = dTable;
            this.dataGridView1.DataSource = bindingSource1;

            DataGridViewLinkColumn editColumn = new DataGridViewLinkColumn();
            editColumn.UseColumnTextForLinkValue = true;
            editColumn.Name = "Edit";
            editColumn.Text = "Edit";
            editColumn.HeaderText = "";
            editColumn.ReadOnly = true;
            editColumn.VisitedLinkColor = Color.Blue;
            this.dataGridView1.Columns.Add(editColumn);

            DataGridViewLinkColumn deleteColumn = new DataGridViewLinkColumn();
            deleteColumn.UseColumnTextForLinkValue = true;
            deleteColumn.Name = "Delete";
            deleteColumn.HeaderText = "";
            deleteColumn.Text = "Delete";
            deleteColumn.ReadOnly = true;
            deleteColumn.VisitedLinkColor = Color.Blue;
            this.dataGridView1.Columns.Add(deleteColumn);

            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            // Change column header name
            this.dataGridView1.Columns["par_name"].HeaderText = "Nama Orang Tua";
            this.dataGridView1.Columns["par_address"].HeaderText = "Alamat Orang Tua";
            this.dataGridView1.Columns["par_job"].HeaderText = "Pekerjaan Orang Tua";
            this.dataGridView1.Columns["par_phone"].HeaderText = "Telepon Orang Tua";
            this.dataGridView1.Columns["par_mobile"].HeaderText = "Ponsel Orang Tua";
            this.dataGridView1.Columns["par_email"].HeaderText = "Email Orang Tua";

            // Set non displayed column
            this.dataGridView1.Columns["par_id"].Visible = false;

            this.dataGridView1.Refresh();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Edit")
            {
               
                if (this.edit == null || this.edit.IsDisposed)
                    edit = new OrtuEdit(this,
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_id"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_name"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_address"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_job"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_phone"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_mobile"].Value.ToString(),
                        this.dataGridView1.Rows[e.RowIndex].Cells["par_email"].Value.ToString());

                edit.MdiParent = this.MdiParent;
                edit.Show();
                edit.Activate();
            }
            else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Delete")
            {
                var result = MessageBox.Show("Apakah Anda ingin menghapus orangtua tersebut ?", "Peringatan !", MessageBoxButtons.OKCancel);
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    DataTable dt = Database.getInstance().tes("select * from Parent");
                    DataRow[] data = dt.Select("par_id='" + this.dataGridView1.Rows[e.RowIndex].Cells["par_id"].Value.ToString() + "'");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i].ItemArray[0].ToString() == data[0].ItemArray[0].ToString())
                        {
                            dt.Rows[i].Delete();
                            Database.getInstance().updateDB(dt);
                            this.dataGridView1.Rows.RemoveAt(e.RowIndex);
                            break;
                        }
                    }
                }
            }

        }
    }
}
